﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using WebAPI.Models;

namespace WebAPI.Controllers
{
    public class GoodsController : BaseApiController
    {
        public IList<Goods> GetSpiltPageList(int goodsTypeId, int sort,int pageIndex, int pageSize)
        {
            List<Goods> spiltPageList = null;
            try
            {
                var list = from g in db.Goods where g.GoodsTypeId == goodsTypeId orderby g.GoodsPostTime descending select g;
                int count = list.Count();
                if (count > 0 && pageIndex >= 1 && pageSize >= 0)
                {
                    spiltPageList = GetList(list, sort, pageIndex, pageSize).ToList();
                }
            }
            catch (Exception e) {
            }

            return spiltPageList;
        }

        [HttpPost]
        public IList<Goods> GetSearchSpiltPageList()
        {
            string searchKey = HttpContext.Current.Request.Form["searchKey"];
            int sort = Convert.ToInt32(HttpContext.Current.Request.Form["searchSort"] ?? string.Empty);
            int pageIndex = Convert.ToInt32(HttpContext.Current.Request.Form["pageIndex"] ?? string.Empty);
            int pageSize = Convert.ToInt32(HttpContext.Current.Request.Form["pageSize"] ?? string.Empty);
            List<Goods> spiltPageList = null;
            try
            {
                var list = from g in db.Goods where g.GoodsName.Contains(searchKey)|| g.GoodsContent.Contains(searchKey) orderby g.GoodsPostTime descending select g;
                int count = list.Count();
                if (count > 0 && pageIndex >= 1 && pageSize >= 0)
                {
                    spiltPageList = GetList(list, sort, pageIndex, pageSize).ToList();
                }
            }
            catch (Exception e)
            {
            }

            return spiltPageList;
        }

        private IList<Goods> GetList(IOrderedQueryable<Goods> list,int sort, int pageIndex, int pageSize) {
            List<Goods> spiltPageList = null;
            //排序
            switch (sort)
            {
                case (int)Enums.Sort.时间降序:
                    list = list.OrderByDescending(x => x.GoodsPostTime);
                    break;
                case (int)Enums.Sort.人气降序:
                    //....
                    break;
                case (int)Enums.Sort.价格升序:
                    list = list.OrderBy(x => x.GoodsMarketPrice);
                    break;
                case (int)Enums.Sort.价格降序:
                    list = list.OrderByDescending(x => x.GoodsMarketPrice);
                    break;
                default:
                    break;
            }

            //分页数据  
            spiltPageList = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            foreach (var item in list)
            {
                int id = item.GoodsId;
                var goodsImg = db.GoodsImages.FirstOrDefault((p) => p.GoodsId == id);
                if (goodsImg != null)
                    item.GoodsDisplayImgUrl = goodsImg.ImageUrl;
            }

            return spiltPageList;
        }
    }
}
